You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[customId].js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { Button, Grid, Tab, Tabs, Typography } from '@mui/material';
  2. import { Box, Container } from '@mui/system';
  3. import { dehydrate, QueryClient } from '@tanstack/react-query';
  4. import Image from 'next/image';
  5. import { useRouter } from 'next/router';
  6. import React, { useState } from 'react';
  7. import Loader from '../../components/loader/Loader';
  8. import TabPanel from '../../components/tab-panel/TabPanel';
  9. import { useFetchSingleProduct } from '../../hooks/useFetchProductData';
  10. import { getProductData } from '../../requests/products/producDataRequest';
  11. import { useStore, useStoreUpdate } from '../../store/cart-context';
  12. const SingleProduct = () => {
  13. const { addCartValue } = useStoreUpdate();
  14. const { cartStorage } = useStore();
  15. const router = useRouter();
  16. const { customId } = router.query;
  17. const { data, isLoading } = useFetchSingleProduct(customId);
  18. // const productCategory = data?.product.category;
  19. const [value, setValue] = useState(0);
  20. const addProductToCart = (quantity) => addCartValue(data.product, quantity);
  21. const inCart = cartStorage?.some(
  22. (item) => item.product.customID === data?.product.customID
  23. )
  24. ? true
  25. : false;
  26. const handleChange = (event, newValue) => {
  27. setValue(newValue);
  28. };
  29. function a11yProps(index) {
  30. return {
  31. id: `simple-tab-${index}`,
  32. 'aria-controls': `simple-tabpanel-${index}`,
  33. };
  34. }
  35. if (isLoading) {
  36. return <Loader loading={isLoading} />;
  37. }
  38. // const productsToShow = (id) => {
  39. // const filtered = shuffle(similarProducts?.productsByCategory)
  40. // .filter((product) => product.customID !== id)
  41. // .slice(0, 3)
  42. // .map((item) => (
  43. // <Grid
  44. // key={item._id}
  45. // item
  46. // lg={4}
  47. // md={6}
  48. // sm={6}
  49. // xs={12}
  50. // sx={{ mb: '100px' }}
  51. // >
  52. // <ProductCard product={item} />
  53. // </Grid>
  54. // ));
  55. // return filtered;
  56. // };
  57. return (
  58. <Box
  59. sx={{
  60. display: 'flex',
  61. flexDirection: 'column',
  62. }}
  63. >
  64. <Container>
  65. <Typography
  66. fontFamily={'body1.fontFamily'}
  67. fontSize="32px"
  68. sx={{ mt: 25, height: '100%', color: 'primary.main' }}
  69. >
  70. {data.product.name}
  71. </Typography>
  72. <Grid container spacing={2}>
  73. <Grid item md={6} sm={12}>
  74. <Image
  75. src="/images/product-card-image.jpg"
  76. alt="product"
  77. width={900}
  78. height={600}
  79. />
  80. </Grid>
  81. <Grid item xs={12} md={6}>
  82. <Tabs
  83. sx={{
  84. '& button:focus': {
  85. borderTop: '1px solid black',
  86. borderLeft: '1px solid black',
  87. borderRight: '1px solid black',
  88. borderRadius: '5px 5px 0 0',
  89. borderBottom: 'none',
  90. },
  91. }}
  92. value={value}
  93. onChange={handleChange}
  94. aria-label="basic tabs example"
  95. >
  96. <Tab
  97. sx={{
  98. width: '50%',
  99. }}
  100. label="Purchase"
  101. {...a11yProps(0)}
  102. />
  103. <Tab sx={{ width: '50%' }} label="Category" {...a11yProps(1)} />
  104. </Tabs>
  105. <TabPanel value={value} index={0}>
  106. <Box flexGrow={2} sx={{ pb: { xs: '70px' } }}>
  107. <Typography>{data.product.description}</Typography>
  108. </Box>
  109. <Box
  110. sx={{
  111. display: { xs: 'flex' },
  112. flexDirection: { xs: 'column' },
  113. justifyContent: { xs: 'center' },
  114. alignItems: { xs: 'center', md: 'flex-end' },
  115. }}
  116. >
  117. <Typography mb={2}>${data.product.price}</Typography>
  118. <Button
  119. disabled={inCart}
  120. onClick={() => addProductToCart(1)}
  121. sx={{
  122. backgroundColor: '#CBA213',
  123. height: 50,
  124. width: { xs: '300px', md: '150px' },
  125. color: 'white',
  126. }}
  127. >
  128. {inCart ? 'In Cart' : 'Add to cart'}
  129. </Button>
  130. </Box>
  131. </TabPanel>
  132. <TabPanel value={value} index={1}>
  133. <Box sx={{ mb: { xs: '60px' } }}>{data.product.category}</Box>
  134. </TabPanel>
  135. </Grid>
  136. </Grid>
  137. <Typography
  138. sx={{
  139. mt: { xs: '60px', md: '100px', lg: '150px' },
  140. mb: 5,
  141. color: 'primary.main',
  142. fontSize: '32px',
  143. }}
  144. >
  145. Other Product You May Like
  146. </Typography>
  147. <Grid container spacing={2}></Grid>
  148. </Container>
  149. </Box>
  150. );
  151. };
  152. export const getServerSideProps = async (context) => {
  153. const { params } = context;
  154. const { customId } = params;
  155. const queryClient = new QueryClient();
  156. await queryClient.prefetchQuery(
  157. ['product', customId],
  158. async () => await getProductData(customId)
  159. );
  160. return {
  161. props: {
  162. dehydratatedState: dehydrate(queryClient),
  163. },
  164. };
  165. };
  166. export default SingleProduct;